package com.alibaba.citrus.ots.unified.inventory.repository.impl;

import javax.annotation.Resource;

import com.alibaba.citrus.ots.unified.inventory.repository.AllocateOrderDetailWriteRepository;
import com.alibaba.fastjson.JSONObject;

import com.epoch.app.otsunifiedinventory.domain.allocateorderdetail.dto.CreateAllocateOrderDetailResponse;
import com.epoch.app.otsunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.otsunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import com.epoch.app.otsunifiedinventory.model.enums.AllocateOrderStatus;
import com.epoch.app.otsunifiedinventory.sdo.AllocateOrderDetailSDO;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: yuhw
 * @date: 2021年09月18日 15:49
 **/
@Component
public class AllocateOrderDetailWriteRepositoryImpl implements AllocateOrderDetailWriteRepository {

    @Resource
    AllocateOrderDetailService allocateOrderDetailService;

    @Override
    public String sync(AllocateOrderDetailSDO allocateOrderDetailSDO) {
        AllocateOrderDetail detail = convert(allocateOrderDetailSDO);
        CreateAllocateOrderDetailResponse r  = allocateOrderDetailService.createAllocateOrderDetail(detail);
        String id = r.getLastInsertId();
        return id ;
    }

    @Override
    public String create(AllocateOrderDetailSDO allocateOrderDetailSDO) {
        AllocateOrderDetail detail = convert(allocateOrderDetailSDO);
        if (detail != null){
            detail.setStatus(AllocateOrderStatus.Shipment_Processing.getValue());
        }
        CreateAllocateOrderDetailResponse r  = allocateOrderDetailService.createAllocateOrderDetail(detail);
        String id = r.getLastInsertId();
        return id ;
    }

    public AllocateOrderDetail convert(AllocateOrderDetailSDO sdo){
        return JSONObject.parseObject(JSONObject.toJSONString(sdo),AllocateOrderDetail.class);
    }
}
